package fem2d;

import math2.MathUtilities;
import fem2.AbstractStructuralStaticDemo;
import fem2.Constraint;
import fem2.Element;
import fem2.MaterialModel;
import fem2.Mesh;
import fem2.MeshPart;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.Observer;
import fem2.StringUtilities;
import fem2.element.StructuralElement;
import fem2.enu.EchoLevelType;
import fem2.enu.State;
import fem2.material.FluencyCriteria;
import fem2.material.LinearIsotropicHardeningRule;
import fem2.material.LinearKinematicHardeningRule;
import fem2.material.VonMisesMM;
import fem2.observer.GidPlasticityObserver;
import fem2.observer.GidStrainObserver;
import fem2.observer.TimeStepObserver;
import fem2.strategies.Strategy;

public class LshapeVonMises extends AbstractStructuralStaticDemo {

	public LshapeVonMises() {
		projectDir = "/home/hbui/kratos_janosch";
		projectName = "LshapeVonMises.gid";
		meshFileName = "structured_mesh";
	}

	@Override
	public Mesh createMesh() {
		return new Lshape().createMesh();
	}

	@Override
	public Model createConditions(Model m) {
		Mesh mesh = m.getMesh();
		Constraint c1 = new Constraint(false, false);
		Constraint c2 = new Constraint(true, false);
		c2.setValue(1, -0.2);
		mesh.setConstraint(c1, MeshUtilities.seekNodesOnSurface(mesh, 0, 1, -1000));
		MeshUtilities.seekNode(mesh, 1000, 500).setConstraint(c2);
		return m;
	}

	@Override
	public MaterialModel createMaterial(Model m) {
		double E = 25850;
		double nu = 0.18;
		double t = 200;
		double ft = 2.7;
		double H = E / 2;
		double K = E / 2;
		FluencyCriteria isoLaw = new LinearIsotropicHardeningRule(ft, K);
		FluencyCriteria kinLaw = new LinearKinematicHardeningRule(H);
		return new VonMisesMM(E, nu, t, 0, State.PLANE_STRAIN, isoLaw, kinLaw);
	}

	@Override
	public Element createElement(MeshPart mp, MaterialModel mm) {
		Element e = new StructuralElement(mp, mm);
		e.setEchoLevel(EchoLevelType.AUX4);
		return e;
	}

	@Override
	public void addObservers(Model m, Strategy s, TimeStepObserver o) {
		super.addObservers(m, s, o);

		String meshName = StringUtilities.getFileName(meshFileName);

		Observer po = new GidPlasticityObserver(m, o, projectDir, projectName, meshName);
		Observer so = new GidStrainObserver(m, o, projectDir, projectName, meshName);

		s.addObserver(so);
		s.addObserver(po);
	}

	public static void main(String[] args) {
		AbstractStructuralStaticDemo demo = new LshapeVonMises();

		double[] lambda = MathUtilities.createLambdaArray(0, 0.1, 1);

		demo.run(lambda);

	}
}
